package com.zenithmind.coding.service.impl;

import com.zenithmind.coding.mapper.ProblemRecommendationMapper;
import com.zenithmind.coding.pojo.entity.Problem;
import com.zenithmind.coding.pojo.vo.ProblemVO;
import com.zenithmind.coding.service.ProblemRecommendationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题推荐服务实现类 - 遵循单一职责原则，专门处理问题推荐相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProblemRecommendationServiceImpl implements ProblemRecommendationService {

    private final ProblemRecommendationMapper problemRecommendationMapper;

    @Override
    public List<ProblemVO> getRecommendProblems(String userId, Integer difficulty, Integer limit) {
        log.info("获取推荐问题: userId={}, difficulty={}, limit={}", userId, difficulty, limit);
        List<Problem> problems = problemRecommendationMapper.selectRecommendProblems(userId, difficulty, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getHotProblems(Integer limit) {
        log.info("获取热门问题: limit={}", limit);
        List<Problem> problems = problemRecommendationMapper.selectHotProblems(limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getRelatedProblems(String problemId, Integer limit) {
        log.info("获取相关问题: problemId={}, limit={}", problemId, limit);
        List<Problem> problems = problemRecommendationMapper.selectRelatedProblems(problemId, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getProblemsByTags(List<String> tags, Integer limit) {
        log.info("根据标签获取问题: tags={}, limit={}", tags, limit);
        List<Problem> problems = problemRecommendationMapper.selectProblemsByTags(tags, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> searchProblems(String keyword, Integer limit) {
        log.info("搜索问题: keyword={}, limit={}", keyword, limit);
        List<Problem> problems = problemRecommendationMapper.searchProblems(keyword, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getDailyChallenges(String userId, Integer count) {
        log.info("获取每日挑战: userId={}, count={}", userId, count);
        String currentDate = java.time.LocalDate.now().toString();
        List<Problem> problems = problemRecommendationMapper.selectDailyChallenges(userId, currentDate, count);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getLearningPath(String userId, Integer pathLength) {
        log.info("获取学习路径: userId={}, pathLength={}", userId, pathLength);
        List<Problem> problems = problemRecommendationMapper.selectLearningPath(userId, pathLength);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getWeaknessReinforcementProblems(String userId, Integer limit) {
        log.info("获取弱项强化问题: userId={}, limit={}", userId, limit);
        List<Problem> problems = problemRecommendationMapper.selectWeaknessReinforcementProblems(userId, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getLevelMatchedProblems(String userId, Integer limit) {
        log.info("获取同级别推荐: userId={}, limit={}", userId, limit);
        List<Problem> problems = problemRecommendationMapper.selectLevelMatchedProblems(userId, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<ProblemVO> getAdvancedChallenges(String userId, Integer limit) {
        log.info("获取进阶挑战: userId={}, limit={}", userId, limit);
        List<Problem> problems = problemRecommendationMapper.selectAdvancedChallenges(userId, limit);
        return problems.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 转换为VO
     */
    private ProblemVO convertToVO(Problem problem) {
        ProblemVO vo = new ProblemVO();
        vo.setId(problem.getId());
        vo.setTitle(problem.getTitle());
        vo.setDescription(problem.getDescription());
        vo.setDifficulty(problem.getDifficulty());
        vo.setTimeLimit(problem.getTimeLimit());
        vo.setMemoryLimit(problem.getMemoryLimit());
        vo.setStatus(problem.getStatus());
        vo.setCreateTime(problem.getCreateTime());
        vo.setUpdateTime(problem.getUpdateTime());
        
        // 设置额外信息
        setAdditionalInfo(vo);
        
        return vo;
    }

    /**
     * 设置额外信息
     */
    private void setAdditionalInfo(ProblemVO vo) {
        // 设置难度名称
        vo.setDifficultyName(getDifficultyName(vo.getDifficulty()));
        
        // 设置状态名称
        vo.setStatusName(getStatusName(vo.getStatus()));
        
        // TODO: 设置其他额外信息，如收藏状态、完成状态等
    }

    /**
     * 获取难度名称
     */
    private String getDifficultyName(Integer difficulty) {
        if (difficulty == null) return "未知";
        return switch (difficulty) {
            case 1 -> "简单";
            case 2 -> "中等";
            case 3 -> "困难";
            case 4 -> "专家";
            default -> "未知";
        };
    }

    /**
     * 获取状态名称
     */
    private String getStatusName(Integer status) {
        if (status == null) return "未知";
        return switch (status) {
            case 0 -> "草稿";
            case 1 -> "已发布";
            case 2 -> "已下架";
            case 3 -> "审核中";
            case 4 -> "审核失败";
            default -> "未知";
        };
    }
}
